package com.decent.ejfadmin.configure.service.impl;

import com.decent.ejfadmin.common.bean.SupConfig;
import com.decent.ejfadmin.common.enums.ErrorCode;
import com.decent.ejfadmin.common.exception.SupException;
import com.decent.ejfadmin.configure.dao.ConfigureDao;
import com.decent.ejfadmin.configure.service.ConfigureService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhangchao
 * @date 2018/7/29 14:56
 */
@Service
public class ConfigureServiceImpl implements ConfigureService {

    @Autowired
    private ConfigureDao configureDao;

    /**
     * 修改参数配置表
     *
     * @param configureId ID
     * @param value       参数值
     * @throws SupException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateConfigValue(Long configureId, String value) throws SupException {
        if (null == configureId || StringUtils.isBlank(value)) {
            throw new SupException(ErrorCode.PARAM_ERROR, "参数有误");
        }
        int result = configureDao.updateConfigValue(configureId, value);
        if (result != 1) {
            throw new SupException(ErrorCode.OPERATE_FAIL, "操作失败");
        }
        //参数配置表提现银行卡id
        Long withdrawBankId = 11L;
        if (withdrawBankId.equals(configureId)) {
            configureDao.updateWithdrawPayBank(value);
        }
    }

    @Override
    public List<SupConfig> findConfigInfo() {
        List<SupConfig> supConfigList = configureDao.findConfigInfo();
        return supConfigList;
    }

    /**
     * 根据key获取配置参数
     *
     * @param key
     * @param state
     * @return
     */
    @Override
    public String getSupConfigByKey(String key, Integer state) {
        return configureDao.getSupConfig(key, state);
    }
}
